package kz.pompei.OneGwt.client.personrole.impl;

import kz.pompei.OneGwt.client.personrole.FormMode;
import kz.pompei.OneGwt.client.personrole.PersonFormMed;
import kz.pompei.OneGwt.client.personrole.PersonFormView;
import kz.pompei.OneGwt.cmodel.PersonDetails;
import kz.pompei.OneGwt.cmodel.PersonRecord;
import kz.pompei.OneGwt.ifaces.util.Comeback;
import kz.pompei.OneGwt.ifaces.util.Handler;
import kz.pompei.OneGwt.ifaces.util.Handler1;
import kz.pompei.OneGwt.remote.PersonroleManagerServiceAsync;

public class PersonFormMedImpl implements PersonFormMed {
  private PersonFormView view;
  private PersonroleManagerServiceAsync service;
  private String personId;
  private Handler1<PersonRecord> saved;
  
  public void setView(PersonFormView view) {
    this.view = view;
  }
  
  @Override
  public void showForm(String personId, Handler1<PersonRecord> saved) {
    this.personId = personId;
    this.saved = saved;
    view.showForm();
    view.setFormMode( personId == null ? FormMode.ADDING :FormMode.EDITING );
    
    initialize();
    
    cleanForm();
    
    if (personId == null) {
      updateEnabilitySaveButton();
    } else {
      view.buttonSave().setEnabled( false );
      service.getPersonDetails( personId, new Comeback<PersonDetails>() {
        @Override
        protected void result(PersonDetails result) {
          write( result );
          updateEnabilitySaveButton();
        }
      } );
      
    }
  }
  
  private boolean initialized = false;
  
  private void initialize() {
    if (initialized) return;
    defineEventHandlers();
    initialized = true;
  }
  
  private void defineEventHandlers() {
    {
      Handler hanlder = new Handler() {
        @Override
        public void handle() {
          updateEnabilitySaveButton();
        }
      };
      view.textSurname().addChangeHandler( hanlder );
      view.textName().addChangeHandler( hanlder );
      view.textAccountId().addChangeHandler( hanlder );
      view.active().addChangeHandler( hanlder );
    }
    
    view.buttonCancel().addClickHandler( new Handler() {
      @Override
      public void handle() {
        view.hideForm();
      }
    } );
    
    view.buttonSave().addClickHandler( new Handler() {
      @Override
      public void handle() {
        save();
      }
    } );
  }
  
  private void write(PersonDetails data) {
    if (data == null) {
      cleanForm();
      return;
    }
    
    view.textSurname().set( data.getSurname() );
    view.textName().set( data.getName() );
    view.textAccountId().set( data.getAccountId() );
    view.active().set( data.isActive() );
  }
  
  private void cleanForm() {
    view.textSurname().set( "" );
    view.textName().set( "" );
    view.textAccountId().set( "" );
    view.active().set( true );
  }
  
  public void setService(PersonroleManagerServiceAsync service) {
    this.service = service;
  }
  
  private void updateEnabilitySaveButton() {
    view.buttonSave().setEnabled( read().isComplete() );
  }
  
  private PersonDetails read() {
    PersonDetails ret = new PersonDetails();
    {
      ret.setId( personId );
      ret.setSurname( view.textSurname().get() );
      ret.setName( view.textName().get() );
      ret.setAccountId( view.textAccountId().get() );
      ret.setActive( view.active().isit() );
    }
    return ret;
  }
  
  private void save() {
    service.savePerson( read(), new Comeback<PersonRecord>() {
      @Override
      protected void result(PersonRecord result) {
        view.hideForm();
        saved.handle( result );
      }
    } );
  }
}
